<template>
  <view>
    <!-- 搜索 -->
    <view class="search-container">
      <my-search bgcolor="#c00000" radius="16"></my-search>
    </view>
    
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, i) in swiperList" :key="i">
        <navigator :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
    
    <!-- 导航 -->
    <view class="cate-container">
      <view class="" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    
    <!-- 楼层UI -->
    <view class="floor-list">
      <!-- 楼层item -->
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 楼层title -->
        <view class="floor-title">
          <image :src="item.floor_title.image_src" mode=""></image>
        </view>
        <!-- 楼层图片 -->
        <view class="floor-image">
          <navigator class="floor-image-left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" mode="widthFix" :style="{width: item.product_list[0].image_width+'rpx'}"></image>
          </navigator>
          <view class="floor-image-right">
            <navigator
              class="floor-image-right-item"
              v-for="(item2, i2) in item.product_list"
              v-if="i2 > 0"
              :key="i2"
              :url="item2.url"
             >
              <image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width+'rpx'}"></image>
            </navigator>
          </view>
        </view>
      </view>
      
    </view>
    
    
  </view>
</template>

<script>
  // 导入设置徽标mixins方法
  import badgeMix from '@/mixins/tabbar-badge.js'
  
  export default {
    mixins: [badgeMix],
    data() {
      return {
        // 轮播图数据
        swiperList: [],
        // 分类数据
        navList: [],
        // 楼层数据
        floorList: [],
      };
    },
    onLoad() {
      // 发起录播图数据请求
      this.getSwiperList()
      // 发起分类数据请求
      this.getNavList()
      // 发起楼层数据请求
      this.getFloorList()
    },
    methods: {
      // 获取轮播图数据
      async getSwiperList() {
        const { data: res } = await uni.$http.get('/home/swiperdata')
        console.log(res)
        // if(res.meta.status !==200){
        //   return uni.showToast({
        //     title: '数据加载错误!',
        //     duration: 1500,
        //     icon: 'none'
        //   })
        // }
        if(res.meta.status !== 200 ) return uni.$showMsg()
        this.swiperList = res.message
      },
      // 获取分类数据
      async getNavList() {
        const { data: res } = await uni.$http.get('/home/catitems')
        console.log(res)
        if(res.meta.status !== 200 ) return uni.$showMsg()
        this.navList = res.message
      },
      // 分类导航
      navClickHandler(item){
        console.log(item)
        if(item.name === '分类'){
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 获取楼层数据
      async getFloorList(){
        const {data: res} = await uni.$http.get('/home/floordata')
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 修改数据格式
        res.message.forEach(floor => {
          floor.product_list.forEach( prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        // 赋值数据到data中
        this.floorList = res.message
      }
    }
  }
</script>

<style lang="scss">
  .search-container {
    position: sticky;
    top: 0;
    z-index: 9;
  }
  swiper {
    height: 320rpx;
    navigator, image {
      width: 100%;
      height: 100%;
    }
  }
  
  .cate-container {
    display: flex;
    justify-content: space-around;
    margin: 32rpx 0;
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }
  
  .floor-list {
    .floor-item{
      .floor-title {
        image {
          height: 60rpx;
          width: 100%;
        }
      }
      .floor-image{
        display: flex;
        padding-left: 8rpx;
        .floor-image-left{
          // margin-left: 8rpx;
        }
        .floor-image-right {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
        }
      }
    }
  }

</style>
